load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_jvm_external//:defs.bzl", "java_export")
load("//bazel:compat.bzl", "SKIP_ON_WINDOWS")

JAZZER_COORDINATES = "com.code-intelligence:jazzer:$(JAZZER_VERSION)"

sh_binary(
    name = "deploy",
    srcs = ["deploy.sh"],
    args = [JAZZER_COORDINATES],
    data = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
    env = {"JAVA_EXECPATH": "$(JAVA)"},
    toolchains = [
        "@bazel_tools//tools/jdk:current_host_java_runtime",
        ":jazzer_version",
    ],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

string_flag(
    name = "jazzer_version",
    build_setting_default = "0.0.0-dev",
    make_variable = "JAZZER_VERSION",
    visibility = ["//visibility:public"],
)

java_export(
    name = "jazzer-api",
    doc_url = "https://codeintelligencetesting.github.io/jazzer-docs/jazzer-api/",
    javadocopts = [
        "-link",
        "https://docs.oracle.com/en/java/javase/17/docs/api/",
    ],
    maven_coordinates = "com.code-intelligence:jazzer-api:$(JAZZER_VERSION)",
    pom_template = "//deploy:jazzer-api.pom",
    toolchains = [":jazzer_version"],
    visibility = ["//visibility:public"],
    runtime_deps = ["//src/main/java/com/code_intelligence/jazzer/api"],
)

java_export(
    name = "jazzer",
    maven_coordinates = JAZZER_COORDINATES,
    pom_template = "jazzer.pom",
    # Do not generate an implicit javadocs target - the current target is based on the shaded deploy
    # JAR, for which the docs JAR generated by default would be empty.
    tags = ["no-javadocs"],
    toolchains = [":jazzer_version"],
    visibility = ["//visibility:public"],
    runtime_deps = [
        "//src/main/java/com/code_intelligence/jazzer:jazzer_import",
    ],
)

alias(
    name = "jazzer-docs",
    actual = "//src/main/java/com/code_intelligence/jazzer:jazzer-docs",
)

alias(
    name = "jazzer-sources",
    actual = "//src/main/java/com/code_intelligence/jazzer:jazzer-sources",
)

java_export(
    name = "jazzer-junit",
    # Exclude the unshaded classes comprising com.code-intelligence:jazzer since the java_library
    # target comprising jazzer-junit depend on the individual libraries, not the shaded jar.
    deploy_env = ["//src/main/java/com/code_intelligence/jazzer:jazzer_lib"],
    doc_deps = [
        ":jazzer-api-docs",
        ":jazzer-docs",
    ],
    doc_url = "https://codeintelligencetesting.github.io/jazzer-docs/jazzer-junit/",
    javadocopts = [
        "-link",
        "https://docs.oracle.com/en/java/javase/17/docs/api/",
        "-link",
        "https://junit.org/junit5/docs/current/api/",
    ],
    maven_coordinates = "com.code-intelligence:jazzer-junit:$(JAZZER_VERSION)",
    pom_template = "jazzer-junit.pom",
    toolchains = [":jazzer_version"],
    visibility = ["//visibility:public"],
    runtime_deps = [
        # These deps' only effect is to include a dependency on the 'jazzer' and 'jazzer-api' Maven artifacts in the
        # POM.
        "//deploy:jazzer",
        "//deploy:jazzer-api",
        "//src/main/java/com/code_intelligence/jazzer/junit",
    ],
)

sh_test(
    name = "jazzer_version_test",
    srcs = ["jazzer_version_test.sh"],
    data = [
        ":jazzer",
        "@bazel_tools//tools/jdk:current_java_runtime",
    ],
    env = {
        "JAVA_EXECPATH": "$(JAVA)",
        "JAZZER_RLOCATIONPATH": "$(rlocationpath :jazzer)",
    },
    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

sh_test(
    name = "jazzer_standalone_version_test",
    srcs = ["jazzer_version_test.sh"],
    data = [
        "//src/main/java/com/code_intelligence/jazzer:jazzer_standalone_deploy.jar",
        "@bazel_tools//tools/jdk:current_java_runtime",
    ],
    env = {
        "JAVA_EXECPATH": "$(JAVA)",
        "JAZZER_RLOCATIONPATH": "$(rlocationpath //src/main/java/com/code_intelligence/jazzer:jazzer_standalone_deploy.jar)",
    },
    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

[
    sh_test(
        name = artifact + "_artifact_test",
        srcs = [artifact + "_artifact_test.sh"],
        args = [
            "$(rootpath :%s)" % artifact,
            "$(JAVABASE)",
        ],
        data = [
            ":" + artifact,
            "@bazel_tools//tools/jdk:current_java_runtime",
        ],
        tags = [
            # Coverage instrumentation necessarily adds files to the jar that we
            # wouldn't want to release and thus causes this test to fail.
            "no-coverage",
        ],
        target_compatible_with = SKIP_ON_WINDOWS,
        toolchains = [
            "@bazel_tools//tools/jdk:current_java_runtime",
        ],
    )
    for artifact in [
        "jazzer-api",
        "jazzer",
        "jazzer-junit",
    ]
]
